package com.lanou.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lanou.entity.TbUser;
import com.lanou.entity.TbUserExample;
import com.lanou.entity.User;
import com.lanou.mapper.TbUserMapper;
import com.lanou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.*;

@Service
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private TbUserMapper tbUserMapper;
    @Autowired
    private JedisPool jedisPool;
    @Override
    public boolean register(TbUser tbUser) {
        Date date = new Date();
        TbUser tbUser1 = new TbUser();
        tbUser1.setUsername(tbUser.getUsername());
        tbUser1.setEmail(tbUser.getEmail());
        tbUser1.setPhone(tbUser.getPhone());
        tbUser1.setCreated(date);
        tbUser1.setUpdated(date);
        tbUser1.setPassword(tbUser.getPassword());
        tbUserMapper.insertSelective(tbUser1);
        return true;
    }
//    @Value("${SESSION_EXPIRE}")
//    private Integer SESSION_EXPIRE;

    @Override
    public User login(String username, String password) {
        Jedis jedis = jedisPool.getResource();
        TbUserExample tbUserExample = new TbUserExample();
        tbUserExample.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<TbUser> tbUsers = tbUserMapper.selectByExample(tbUserExample);
        if (tbUsers.size() >  0) {
            TbUser user = tbUsers.get(0);
            TbUser tbUser = new TbUser();
            tbUser.setUsername(user.getUsername());
            tbUser.setId(user.getId());
            String tbUserString = JSON.toJSONString(tbUser);
            System.out.println(tbUserString);
            //将用户信息写入redis
            String token = UUID.randomUUID().toString();
            jedis.setex("session:" + token,1800,tbUserString);
            jedis.close();
            User user1 = new User();
            user1.setId(user.getId());
            user1.setToken(token);
            return user1;
        }else {
            return null;
        }
    }

    @Override
    public TbUser getUserByToken(String token) {
        Jedis jedis = jedisPool.getResource();
        String s = jedis.get("session:"+token);
        if (s != null){
            jedis.expire("重新部署时间:"+token,1800);
        }
        TbUser user = JSON.parseObject(s, TbUser.class,SerializerFeature.WRITE_MAP_NULL_FEATURES);
        jedis.close();
        return user;
    }

    @Override
    public TbUser outUserByToken(String token) {
       Jedis jedis = jedisPool.getResource();
        String s = jedis.get("session:" + token);
        jedis.del("session:" +token);
        TbUser user = JSON.parseObject(s, TbUser.class,SerializerFeature.WRITE_MAP_NULL_FEATURES);
        jedis.close();
        return user;
    }

}
